use crate::commands::list::List;
use crate::storage::{InsertTodoItem, Storage};
use crate::ExecuteCommand;
use chrono::{Local, NaiveDate};
use clap::Parser;

#[derive(Parser, Debug, Clone)]
pub struct Add {
    /// 待办事项的内容（必填）
    pub content: String,

    /// 将待办设置为最高优先级（可选）
    #[arg(short = 't', long = "top")]
    pub top_priority: bool,

    /// 指定待办的日期
    #[arg(short,long,value_parser=crate::commands::parse_date)]
    pub date: Option<NaiveDate>,

    /// 添加后展示今天的待办
    #[arg(short = 'l', long = "list")]
    pub show_list: bool,
}

impl ExecuteCommand for Add {
    fn execute_command(&self, storage: &impl Storage) -> anyhow::Result<()> {
        let insert_todo_item = InsertTodoItem {
            content: self.content.clone(),
            date: self.date.clone().or_else(|| Some(Local::now().date_naive())).unwrap(),
            priority: if self.top_priority {
                u8::MAX
            } else {
                0
            },
        };

        storage.insert(&insert_todo_item)?;

        if self.show_list {
            let list = List {
                days: None,
                status: None,
            };
            list.execute_command(storage)?;
        }

        Ok(())
    }
}